package com.zh.controller;


import com.alibaba.fastjson.JSON;
import com.zh.common.service.RabbitMQService;
import com.zh.config.rabbitmq.TopicRabbitConfig;
//import io.swagger.annotations.Api;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 发送消息控制器
 */
@RestController
//@Api("rabbitmq相关的api")
public class SendMessageController {
    private static String testDirectExchange = "test-direct-exchange";
    private static String testDirectRouting = "test-direct-routing";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RabbitMQService rabbitMQService;

    @GetMapping("/send-direct/{message}")
    public String sendDirectMessage(@PathVariable String message){
        rabbitMQService.sendMsgToDirect(testDirectExchange,testDirectRouting,message);
        return "ok";
    }

    @GetMapping("/send-topic-man/{message}")
    public String sendTopicMessage(@PathVariable String message){
        rabbitMQService.sendMsgToTopic(TopicRabbitConfig.TEST_WOMAN_TOPIC_EXCHANGE
                ,TopicRabbitConfig.MAN
                ,message);
        return "ok";
    }

    @GetMapping("/send-topic-woman/{message}")
    public String sendTopic2Message(@PathVariable String message){
        rabbitMQService.sendMsgToTopic(TopicRabbitConfig.TEST_WOMAN_TOPIC_EXCHANGE
                ,TopicRabbitConfig.WO_MAN
                ,message);
        return "ok";
    }

    @GetMapping("/send-fanout/{message}")
    public String sendFanoutMessage(@PathVariable String message){
        rabbitMQService.sendMsgToFanout("test-fanout-exchange",message);
        return "ok";
    }

    /**
     * 测试消息回调，这个交换机是没有的。
     * @return
     */
    @GetMapping("/TestMessageAck")
    public String TestMessageAck() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: non-existent-exchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("non-existent-exchange", "TestDirectRouting", map);
        return "ok";
    }

    /**
     * 测试消息回调，这个交换机有，但是没有绑定任何队列。
     * @return
     */
    @GetMapping("/TestMessageAck2")
    public String TestMessageAck2() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: lonelyDirectExchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("lonelyDirectExchange", "TestDirectRouting", map);
        return "ok";
    }

    /**
     * 测试死信队列
     *
     * @return
     */
    @GetMapping("/TestMessageAck3")
    public String TestMessageAck3() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: lonelyDirectExchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("test-dead-exchange", "testDeadRouting", JSON.toJSONString(map));
        return "ok";
    }

    /**
     * 测试死信队列3
     *
     * @return
     */
    @GetMapping("/TestMessageAck33")
    public String TestMessageAck33() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: lonelyDirectExchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);

        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setExpiration("10000"); // 消息的过期属性，单位 ms

        Message message = new Message("这条消息 10 秒后过期".getBytes(), messageProperties);
        rabbitTemplate.send("test-dead-exchange3", "testDeadRouting3", message);

        rabbitTemplate.convertAndSend("test-dead-exchange3", "testDeadRouting3", JSON.toJSONString(map));
        return "ok";
    }

    @GetMapping("/send/{msg}")
    public String  sendMsg(@PathVariable String msg){
        rabbitTemplate.convertAndSend("dead.letter.demo.simple.business.exchange","",msg);
        return "ok";
    }

    @GetMapping("/deal4")
    public String sendMsg4(@RequestParam("msg") String msg,@RequestParam("time") Integer time) {

        rabbitTemplate.convertAndSend("test-dead-exchange4","testDeadRouting4", msg,message -> {
                    //注意这里时间要是字符串形式
                    message.getMessageProperties().setExpiration(time+"");
                    return message;
                });
        return "ok";
    }
}
